//  program:	Create replication file for partisan committees--appendix
//  task:       *create replication file for most tables and models in appendix of submitted version of paper
				*some other tables are created using r files, see related files. Replication of permutation occurs in separatea stata file.
				*data developed in r
//				project: Committee Partisanship
// 				Related files: 03replication_vx_date

//  author:     Josh Ryan \ December 29, 2019

// #Start
// program setup



use "clogit_final_v3_030720.dta", clear

*scale partydiff
replace partydiff=partydiff/100

replace partydiff_wo=partydiff_wo/100


*Robustness Check in paper text: Fixed Effects Logit Estimates of Committee Assignment only for first term members

*both district partisanship measures--actual models in the paper
eststo c1: logit commassignid c.lnkernelldispart##c.partydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if numterm==1, cluster(stewartcommid)

eststo c2: logit commassignid c.lndispart##c.partydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if numterm==1, cluster(stewartcommid)

*Figure: Predicted Probabilities of Committee Choice Conditional on Kernell District Ideology and Average Difference in Partisan Voting

*marginal effects, pred probs
logit commassignid c.lnkernelldispart##c.partydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if numterm==1, cluster (stewartcommid)
eststo cp1: margins, at(lnkernelldispart=(-6.907755) partydiff_wo=(0(.25)1)) post vsquish 


logit commassignid c.lnkernelldispart##c.partydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if numterm==1, cluster(stewartcommid)
eststo cp2: margins, at(lnkernelldispart=(1.233143) partydiff_wo=(0(.25)1)) post vsquish 


*subsetted only for congresses in which there was not a large seat change between the parties



*Table: The Effects of District Partisanship on Partisan Committee Membership|Difference in Party Voting
*Appendix A4
use "datwcommpart_v5_030720.dta", clear

*label var clusterid "id, district-state-redist cycle 0 prior 2000 1 after 2000 2 after 2010"
*label var distcong "id, district-state-congress"

eststo m1: mixed partydiff_wo c.lnkernelldispart rankwithinparty female membermajparty memberparty commleader commseniority permaj grosewart fkpolicy fkprestige || cong: , cov(unstruct) level(90)
eststo m2: mixed partydiff_wo c.lndispart rankwithinparty female membermajparty memberparty commleader commseniority permaj grosewart fkpolicy fkprestige || cong: , cov(unstruct)

estout m1 m2, style(tex) cells(b(star fmt(2)) se(par)) starlevels($^\#$ .1 * 0.05) stats(aic N r2) varlabels( ///
lnkernelldispart	"Logged Kernell District Partisanship" lndispart "Logged Presidential Vote District Partisanship" votepct "Member Vote Percentage" ///
rankwithinparty "Committee Party Seniority Rank" commseniority "Committee Overall Seniority"  female "Gender (Female=1)" numterms "Number of Terms Served" membermajparty "Majority Party Member (Yes=1)" ///
memberparty "Member Party (Dem.=1)" commleader "Chair or Ranking Member of Any Committee" permaj "Maj. Party Percent of Committee Seats" ///
grosewart "Grosewart Committee Value" fkpolicy "Policy Committee" fkprestige "Prestige Committee") 



*Appendix A5: Table A5, Robustness Check: Regression of District Partisanship on Partisan Committee Membership|Average Difference in Percentage of Each Party Voting Yes
eststo m3: reg partydiff_wo c.lnkernelldispart rankwithinparty female membermajparty memberparty commleader commseniority permaj grosewart fkpolicy fkprestige i.cong, r
eststo m4: reg partydiff_wo c.lndispart rankwithinparty female membermajparty memberparty commleader commseniority permaj grosewart fkpolicy fkprestige i.cong, r

estout m3 m4, style(tex) cells(b(star fmt(2)) se(par)) starlevels($^\#$ .1 * 0.05) stats(aic N r2) varlabels( ///
lnkernelldispart	"Logged Kernell District Partisanship" lndispart "Logged Presidential Vote District Partisanship" votepct "Member Vote Percentage" ///
rankwithinparty "Committee Party Seniority Rank" commseniority "Committee Overall Seniority"  female "Gender (Female=1)" numterms "Number of Terms Served" membermajparty "Majority Party Member (Yes=1)" ///
memberparty "Member Party (Dem.=1)" commleader "Chair or Ranking Member of Any Committee" permaj "Maj. Party Percent of Committee Seats" ///
grosewart "Grosewart Committee Value" fkpolicy "Policy Committee" fkprestige "Prestige Committee") 



*Robustness check mentioned in text. Keeping only Congress in which the party does not lose a significant number of seats:
use "datwcommpart_v5_030720.dta", clear

gen fewpartyloss=0
replace fewpartyloss=1 if cong==104 | cong==105 | cong==106 | cong==107 | cong==108 | cong==109|cong==113

*Table: The Conditional Effect of Outlying District Preferences on Committee Departure

keep if fewpartyloss==1

eststo int1: logit memberleft c.abszk##c.votepct rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
margins, at(abszk=(0) votepct=(40(10)100)) post vsquish

logit memberleft c.abszk##c.votepct rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
margins, at(abszk=(4.6) votepct=(40(10)100)) post vsquish


eststo int2: logit memberleft c.abszk##c.kernelldispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
margins, at(abszk=(0) kernelldispart=(0(1)3.4)) level(90)

logit memberleft c.abszk##c.kernelldispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
margins, at(abszk=(2) kernelldispart=(0(1)3.4)) level(90)


eststo int3: logit memberleft c.abszk##c.dispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
eststo pp1: margins, at(abszk=(0) dispart=(0(10)50)) post vsquish

logit memberleft c.abszk##c.dispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
eststo pp2: margins, at(abszk=(4.6) dispart=(0(10)50)) post vsquish

estout int1 int2 int3, style(tex) cells(b(star fmt(2)) se(par)) starlevels($^\#$ .1 * 0.05) stats(aic N r2_p) varlabels( ///
abszk "Member Outlierness (Kernell)" votepct "Member Vote Percentage" kernelldispart "Kernell District Partisanship" dispart "Presidential Vote District Partisanship" ///
rankwithinparty "Committee Party Seniority Rank" female "Gender (Female=1)" membermajparty "Majority Party Member (Yes=1)" ///
memberparty "Member Party (Dem.=1)" commleader "Chair or Ranking Member of Any Committee")

gen appropcomm=0
replace appropcomm=1 if fkconstit==1 
replace appropcomm=1 if stewartcommid==104

eststo absapprop3: logit memberleft c.abszk##c.dispart##i.appropcomm rankwithinparty female membermajparty memberparty commleader grosewart permaj i.cong if numterms<4, cluster(stewartcommid)
eststo pp1: margins, at(appropcomm=(0) abszk=(0) dispart=(0(10)50)) post vsquish

logit memberleft c.abszk##c.dispart##i.appropcomm rankwithinparty female membermajparty memberparty commleader grosewart permaj i.cong if numterms<4, cluster(stewartcommid)
eststo pp2: margins, at(appropcomm=(1) abszk=(0) dispart=(0(10)50)) post vsquish

eststo absapprop3: logit memberleft c.abszk##c.dispart##i.appropcomm rankwithinparty female membermajparty memberparty commleader grosewart permaj i.cong if numterms<4, cluster(stewartcommid)
eststo pp3: margins, at(appropcomm=(0) abszk=(4.6) dispart=(0(10)50)) post vsquish

logit memberleft c.abszk##c.dispart##i.appropcomm rankwithinparty female membermajparty memberparty commleader grosewart permaj i.cong if numterms<4, cluster(stewartcommid)
eststo pp4: margins, at(appropcomm=(1) abszk=(4.6) dispart=(0(10)50)) post vsquish


*Robustness check mentioned in text. Removing two committees with few votes, Veterans Affairs and Small Business:
use "clogit_final_v3_030720.dta", clear

*scale partydiff
replace partydiff=partydiff/100
replace lagpartydiff=lagpartydiff/100

replace partydiff_wo=partydiff_wo/100
replace lagpartydiff_wo=lagpartydiff_wo/100

drop if stewartcommid==184
drop if stewartcommid==192

*with kernell district ideology
*for reference to compare to fixed effects logit
clogit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.cong, group(icpsr)
*number of distinct obs used in analysis
egen icpsr_comm_cong = concat(icpsr stewartcommid cong) if commassignid==1
qui tab icpsr_comm_cong if e(sample)
di r(r)

*with pres. district partisanship
clogit commassignid c.dispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong, group(icpsr)
qui tab icpsr_comm_cong if e(sample)
di r(r)


eststo c1: logit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong, cluster(stewartcommid)
egen icpsr_comm_cong = concat(icpsr stewartcommid cong) if commassignid==1
qui tab icpsr_comm_cong if e(sample)
di r(r)


eststo c2: logit commassignid c.lndispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong, cluster(stewartcommid)
qui tab icpsr_comm_cong if e(sample)
di r(r)

*marginal effects, pred probs
logit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong, cluster (stewartcommid)
eststo cp1: margins, at(lnkernelldispart=(-6.907755) lagpartydiff_wo=(0(.25)1)) post vsquish 


logit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong, cluster(stewartcommid)
eststo cp2: margins, at(lnkernelldispart=(1.233143) lagpartydiff_wo=(0(.25)1)) post vsquish 




*Table: The Conditional Effect of Distributive Committees on District Partisanship and Partisan Committee Membership


gen appropcomm=0
replace appropcomm=1 if fkconstit==1 
replace appropcomm=1 if stewartcommid==104


eststo dist_m1: logit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if appropcomm==0, cluster(stewartcommid)
eststo dist1: margins, at(lnkernelldispart=(-6.907755) lagpartydiff_wo=(0(.25)1)) post vsquish 
egen icpsr_comm_cong = concat(icpsr stewartcommid cong) if commassignid==1
qui tab icpsr_comm_cong if e(sample)
di r(r)

eststo dist_m2: logit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if appropcomm==1, cluster(stewartcommid)
eststo dist2: margins, at(lnkernelldispart=(-6.907755) lagpartydiff_wo=(0(.25)1)) post vsquish 
qui tab icpsr_comm_cong if e(sample)
di r(r)

eststo dist_m3: logit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if appropcomm==0, cluster(stewartcommid)

eststo dist3: margins, at(lnkernelldispart=(1.233143) lagpartydiff_wo=(0(.25)1)) post vsquish 
eststo dist_m4: logit commassignid c.lnkernelldispart##c.lagpartydiff_wo rankwithinparty commseniority permaj i.stewartcommid i.cong if appropcomm==1, cluster(stewartcommid)
eststo dist4: margins, at(lnkernelldispart=(1.233143) lagpartydiff_wo=(0(.25)1)) post vsquish 



*Table: The Conditional Effect of Outlying District Preferences on Committee Departure

use "datwcommpart_v4_051419.dta", clear

drop if stewartcommid==184
drop if stewartcommid==192


eststo int1: logit memberleft c.abszk##c.votepct rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
margins, at(abszk=(0) votepct=(40(10)100)) post vsquish

logit memberleft c.abszk##c.kernelldispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong if numterms<4, cluster(stewartcommid)
margins, at(abszk=(2) kernelldispart=(0(1)3.4)) level(90)


*Table: The Conditional Effect of Outlying District Preferences on Committee Departure

use "datwcommpart_v4_051419.dta", clear

gen appropcomm=0
replace appropcomm=1 if fkconstit==1 
replace appropcomm=1 if stewartcommid==104

drop if stewartcommid==184
drop if stewartcommid==192


eststo absapprop1: logit memberleft c.abszk##c.votepct##i.appropcomm rankwithinparty female membermajparty memberparty commleader grosewart permaj i.cong if numterms<4, cluster(stewartcommid)
margins, at(abszk=(0) votepct=(40(10)100)) post vsquish


eststo absapprop2: logit memberleft c.abszk##c.kernelldispart##i.appropcomm rankwithinparty female membermajparty memberparty commleader grosewart permaj i.cong if numterms<4, cluster(stewartcommid)
margins, at(appropcomm=(0) abszk=(0) kernelldispart=(0(1)3.4)) level(90)



*Robustness Check, Table A8: The Conditional Effect of Outlying District Party Preferences on Committee Departure--Copartisans Only

use "datwcommpart_v5_030720.dta", clear

eststo int1: logit memberleft c.abszk_party##c.votepct rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong numterms, cluster(stewartcommid)
margins, at(abszk_party=(.00017) votepct=(40(10)100)) post vsquish

logit memberleft c.abszk_party##c.votepct rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong numterms, cluster(stewartcommid)
margins, at(abszk_party=(4.28) votepct=(40(10)100)) post vsquish


eststo int2: logit memberleft c.abszk_party##c.kernelldispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong numterms, cluster(stewartcommid)
margins, at(abszk=(0) kernelldispart=(0(1)3.4)) level(90)

logit memberleft c.abszk_party##c.kernelldispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong numterms, cluster(stewartcommid)
margins, at(abszk=(2) kernelldispart=(0(1)3.4)) level(90)


eststo int3: logit memberleft c.abszk_party##c.dispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong numterms, cluster(stewartcommid)
eststo pp1: margins, at(abszk=(0) dispart=(0(10)50)) post vsquish

logit memberleft c.abszk_party##c.dispart rankwithinparty female membermajparty memberparty commleader i.stewartcommid i.cong numterms, cluster(stewartcommid)
eststo pp2: margins, at(abszk=(4.6) dispart=(0(10)50)) post vsquish

estout int1 int2 int3, style(tex) cells(b(star fmt(2)) se(par)) starlevels($^\#$ .1 * 0.05) stats(aic N r2_p) varlabels( ///
abszk_party "Member Party Outlierness (Kernell)" votepct "Member Vote Percentage" kernelldispart "Kernell District Partisanship" dispart "Presidential Vote District Partisanship" ///
rankwithinparty "Committee Party Seniority Rank" female "Gender (Female=1)" membermajparty "Majority Party Member (Yes=1)" ///
memberparty "Member Party (Dem.=1)" commleader "Chair or Ranking Member of Any Committee" numterms "Number of Terms Served")

*margins are here:
use "marginaleffects_zdispart_party_072120_v1.dta", clear
gen interactmerge=99

*gen var to merge on so i can get the rest of the data in with the marginal effects to generate histogram
*but i want to limit the marginal effects to only the first13 observations so create id that i can use to subset (actually doesn't matter for drawing purposes but I think
*it looks cleaner
merge m:m interactmerge using "datwcommpart_v5_030720.dta" 
gen id=_n

graph twoway (hist dispart, width(1) percent color(gs14) yaxis(2)) ///
(connected margin estdis if estabszk==0, sort lcolor(gray) lpattern(solid) lwidth(thin) msymbol(none) yaxis(1)) ///
(connected lci estdis if estabszk==0, sort lcolor(gray) lpattern(dash) lwidth(medium) msymbol(none)) ///
(connected uci estdis if estabszk==0, sort lcolor(gray) lpattern(dash) lwidth(medium) msymbol(none)) ///
(connected margin estdis if estabszk==4& id<13, sort lcolor(black) lpattern(solid) lwidth(thick) msymbol(none) yaxis(1)) ///
(connected lci estdis if estabszk==4 & id<13, sort lcolor(gray) lpattern(dash) lwidth(medium) msymbol(none)) ///
(connected uci estdis if estabszk==4& id<13 , sort lcolor(gray) lpattern(dash) lwidth(medium) msymbol(none)), ///
ytitle("Predicted Pr(Committee Departure)", axis(1)) ytitle("Density of Dist. Partisanship", axis(2)) xtitle("Pres. Vote District Extremity") ///
 ylabel(0(.1)1, axis (1) valuelabel) ylabel(0(2)10, axis (2) valuelabel) yscale(alt) yscale(alt axis(2)) ///
xlabel(0(5)50, valuelabel) legend(col(1) order(2 "Pred. Prob. Min. Party Outlier" 5 "Pred. Prob. Max. Party Outlier" 3 "95% Confidence Interval" ) ///
bplacement(nw) ring(1) size(small)) scheme(s1mono) graphregion(fcolor(white) ifcolor(white)) title("")
graph save outlierinteract_party.gph, replace





